package com.masterclient.app.modules.shop;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.adapter.StockDetailsItemAdapter;
import com.masterclient.app.modules.shop.beans.StockDetailsBean;
import com.masterclient.app.modules.shop.beans.StockExplainBean;
import com.masterclient.app.modules.shop.contract.StockDetailsContract;
import com.masterclient.app.modules.shop.contract.StockDetailsPresenter;
import com.masterclient.app.modules.shop.widget.StockExplainDialog;
import com.masterclient.app.modules.shop.widget.UpdateStockDialog;
import butterknife.BindView;

/**
 * @auther hxm
 * @date 2020/4/8.
 */
@Route(path = AroutePath.STOCK_DETAILS_FRAGMENT)
public class StockDetailsFragment extends BaseFragment<StockDetailsPresenter> implements StockDetailsContract.View {

    public static final String PRODUCT_ID = "productId";

    @BindView(R.id.recycler)
    RecyclerView recyclerView;

    private TextView tvBottomTitle;
    private TextView tvBottomAllStock;
    private TextView tvBottomHomeStock;
    private TextView tvBottomOtherStock;
    private StockDetailsItemAdapter stockDetailsItemAdapter;
    private CallBack callBack;
    private int type;
    private StockDetailsBean dataBean;

    private UpdateStockDialog updateStockDialog;

    private String productId;

    public static StockDetailsFragment getInstance(String productId){
        StockDetailsFragment fragment = new StockDetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PRODUCT_ID,productId);
        fragment.setArguments(bundle);
        return fragment;
    }

    public interface CallBack{
        void getUrl(String url);
    }

    public void setCallBackListener(CallBack callBack){
        this.callBack = callBack;
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_stock_details, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();

        productId = getArguments().getString(PRODUCT_ID);
        stockDetailsItemAdapter = new StockDetailsItemAdapter();
        stockDetailsItemAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.view_home_stock) {
                StockDetailsBean.SkuItemBean skuItemBean = (StockDetailsBean.SkuItemBean) adapter.getData().get(position);
                updateStockDialog = new UpdateStockDialog(getActivity(),dataBean.modifyNotice,skuItemBean.homeStock,skuItemBean.holdQty);
                updateStockDialog.setOnUpdateStockListener((quantity, remark) -> {
                    mPresenter.updateStockNum(skuItemBean.sku_id,quantity,remark,skuItemBean.homeStock);
                });
                updateStockDialog.show();
            }
        });
        View headView = getLayoutInflater().inflate(R.layout.item_head_stock_details, null);
        headView.findViewById(R.id.tv_home_stock).setOnClickListener(v -> {
            type = 1;
            mPresenter.getExplain();
        });
        headView.findViewById(R.id.tv_other_stock).setOnClickListener(v -> {
            type = 2;
            mPresenter.getExplain();
        });
        View footerView = getLayoutInflater().inflate(R.layout.item_head_stock_details, null);
        tvBottomTitle = footerView.findViewById(R.id.tv_title);
        tvBottomAllStock = footerView.findViewById(R.id.tv_all_stock);
        tvBottomHomeStock = footerView.findViewById(R.id.tv_home_stock);
        tvBottomOtherStock = footerView.findViewById(R.id.tv_other_stock);
        View viewItem = footerView.findViewById(R.id.view_item);
        viewItem.setBackgroundColor(Color.parseColor("#ffffff"));
        stockDetailsItemAdapter.addHeaderView(headView);
        stockDetailsItemAdapter.addFooterView(footerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        stockDetailsItemAdapter.bindToRecyclerView(recyclerView);

        mPresenter.getStockList(productId);
    }

    @Override
    public void showData(StockDetailsBean dataBean) {
        if (dataBean != null) {
            this.dataBean = dataBean;
            if (!TextUtils.isEmpty(dataBean.explainUrl)){
                callBack.getUrl(dataBean.explainUrl);
            }
            if (dataBean.skuList != null && dataBean.skuList.size() > 0) {
                recyclerView.setVisibility(View.VISIBLE);
                stockDetailsItemAdapter.setNewData(dataBean.skuList);
            }
            if (dataBean.statistics != null) {
                tvBottomTitle.setText(dataBean.statistics.title);
                tvBottomAllStock.setText(dataBean.statistics.totalStock);
                tvBottomHomeStock.setText(dataBean.statistics.homeStock);
                tvBottomOtherStock.setText(dataBean.statistics.storeStock);
                tvBottomHomeStock.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                tvBottomOtherStock.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            }
        } else {
            recyclerView.setVisibility(View.GONE);
        }
    }

    @Override
    public void showExplainData(StockExplainBean dataBean) {
        if (dataBean != null){
            StockExplainDialog stockExplainDialog = new StockExplainDialog(getActivity(), dataBean, type);
            stockExplainDialog.show();
        }
    }

    @Override
    public void showDataFailed(String msg) {
        recyclerView.setVisibility(View.GONE);
        ToastUtil.showToast(msg);
    }

    @Override
    public void showExplainFailed(String msg) {
        ToastUtil.showToast(msg);
    }

    @Override
    public void updateSuccess() {
        if (updateStockDialog != null){
            updateStockDialog.dismiss();
            mPresenter.getStockList(productId);
        }
    }

    @Override
    public void updateFailed(String msg) {
        ToastUtil.showToast(msg);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
